﻿using StudentWeb.DAL;
using StudentWeb.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StudentWeb.BLL
{
    public class CourseBO
    {
        #region 课程信息 分页列表
        /// <summary>
        /// 课程信息 分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="wc"></param>
        /// <returns></returns>
        public IResult GetCourseList(int pageIndex, int pageSize, string wc = null)
        {
            var list = new CourseDataDAO().GetList(pageIndex, pageSize, wc);
            return new DataResult<PagedList<CourseData>>(list);
        }
        #endregion
        #region 课程信息 单条记录
        /// <summary>
        /// 课程信息 单条记录
        /// </summary>
        /// <param name="courseDataID"></param>
        /// <returns></returns>
        public IResult GetCourseInfo(int courseDataID)
        {
            var res = new CourseDataDAO().GetInfo(courseDataID);
            return new DataResult<CourseData>(res);
        }
        #endregion
        #region 课程信息 保存
        public bool SaveCourseInfo(dynamic data, int userid)
        {
            int courseDataID = data.CourseDataID ?? 0;
            var result = 0;
            if (courseDataID > 0)
            {
                var record = new CourseDataDAO().GetInfo(courseDataID);
                record.CourseNo = data.CourseNo;
                record.CourseState = data.CourseState;
                record.CourseName = data.CourseName;
                record.CourseType = data.CourseType;
                record.CourseCredit = data.CourseCredit;
                record.CourseTerm = data.CourseTerm;
                record.CourseInfo = data.CourseInfo;
                record.RegDateTime = DateTime.Now;
                record.RegUserID = userid;
                // record.EditDateTime = DateTime.Now;
                // record.EditUserID = userid;
                result = new CourseDataDAO().Update(record);
            }
            else if (courseDataID == 0)
            {
                var Course = new CourseData()
                {
                    CourseDataID=1,
                    CourseNo = data.CourseNo,
                    CourseState = data.CourseState,
                    CourseName = data.CourseName,
                    CourseType = data.CourseType,
                    CourseCredit = data.CourseCredit,
                    CourseTerm = data.CourseTerm,
                    CourseInfo = data.CourseInfo,
                    RegDateTime = DateTime.Now,
                    RegUserID = userid,
                    EditDateTime = DateTime.Now,
                    EditUserID = userid,
                };
                result = new CourseDataDAO().Insert(Course);
            }
            return result > 0;
        }
        #endregion
        #region 设置课程状态
        public IResult SetCourseState(int courseDataID, int courseState)
        {
            var record = new CourseDataDAO().GetInfo(courseDataID);
            if (record == null)
            {
                return new Result(ResultCode.Fail, "未找到此ID的课程");
            }
            int newState = 0;
            if (courseState == 0)
            {
                newState = 1;
            }
            else if (courseState == 1)
            {
                newState = 0;
            }
            record.CourseState = newState;
            var result = new CourseDataDAO().Update(record);
            return result >= 1 ? new Result() : new Result(ResultCode.Fail, "修改失败");
        }
        #endregion
    }
}
